package cn.itcast.base.J_常用工具之API.Date.demo;


import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

//java获取当前时间的方法
//https://www.jb51.net/article/169330.htm
public class Test3 {
    public static void main(String[] args) {
        System.out.println("System.currentTimeMillis()");
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));

        System.out.println("java.util.Date");
        Date date1 = new Date(); // this object contains the current date value
        System.out.println(date1.getTime());
        SimpleDateFormat formatter1 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        SimpleDateFormat formatter11 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(formatter1.format(date1));
        System.out.println("time:"+formatter11.format(date1));


        System.out.println("Calendar API");
        Calendar calendar = Calendar.getInstance(); // gets current instance of the calendar
        SimpleDateFormat formatter2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        System.out.println(formatter2.format(calendar.getTime()));


        System.out.println("Date/Time API");
        LocalDate date3 = LocalDate.now(); // gets the current date
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("dd-MM-yyyy");
        System.out.println(date3.format(formatter3));


        System.out.println("LocalTime");
        LocalTime time = LocalTime.now(); // gets the current time
        DateTimeFormatter formatter4 = DateTimeFormatter.ofPattern("HH:mm:ss");
        System.out.println(time.format(formatter4));


        System.out.println("LocalDateTime");
        LocalDateTime dateTime = LocalDateTime.now(); // gets the current date and time
        DateTimeFormatter formatter5 = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        System.out.println(dateTime.format(formatter5));
    }
}
